<?php

$old_error_reporting = error_reporting(E_ALL & ~E_NOTICE);

require_once("functions.php");
connectToDb();

$privateToken = $_REQUEST["privateToken"];
$name = $_REQUEST["name"];
$content = json_encode($_REQUEST["content"]);
$description = $_REQUEST["description"];
$imageData = $_REQUEST["image"];
$email = $_REQUEST["email"];
$sizeWidth = $_REQUEST["sizeWidth"];
$sizeHeight = $_REQUEST["sizeHeight"];

function saveImage($base64imageData, $imageToken) {
    $imageIdentifier = md5(time());
    $fullFilename = ($fullFilename ? $fullFilename : "images/designs/" . $imageToken . ".png");

    file_put_contents($fullFilename, base64_decode(substr($base64imageData, strpos($base64imageData, ",") + 1)));

    return $fullFilename;
}

function saveThumbnailFromImage($imageFilename) {
    
    // Load the existing image
    $image = imagecreatefrompng($imageFilename);
    
    // Do the resize
    list($currentWidth, $currentHeight) = getimagesize($imageFilename);
    $newHeight = 100;
    $newWidth = round(($currentWidth / $currentHeight) * $newHeight);
    $output = imagecreatetruecolor($newWidth, $newHeight);
    imagefilledrectangle($output, 0, 0, $newWidth, $newHeight, imagecolorallocate($output, 255, 255, 255)); // White background
    imagecopyresized($output, $image, 0, 0, 0, 0, $newWidth, $newHeight, $currentWidth, $currentHeight);
    
    // Save the new file
    imagealphablending($output, false);
    imagesavealpha($output, true);
    $imageThumbnailFilename = str_replace(".png", "", $imageFilename) . "_thumb.png";
    imagepng($output, $imageThumbnailFilename);

    return $imageThumbnailFilename;
}

if ($privateToken != '') {
    // Token provided - this is an update

    // Update the image contents
    if ($imageData) {
        $fullFilename = saveImage($imageData, md5($privateToken));
        $thumbnailFilename = saveThumbnailFromImage($fullFilename);
    }
    
	$updateQuery =
       "UPDATE design
        SET name = '" . mysql_real_escape_string($name) . "',
            content = '" . mysql_real_escape_string($content) . "',
            description = '" . mysql_real_escape_string($description) . "',
            size_width = '" . mysql_real_escape_string($sizeWidth) . "',
            size_length = '" . mysql_real_escape_string($sizeHeight) . "',
            edited_date = CURRENT_DATE(),
            image = '$fullFilename',
            thumbnail_image = '$thumbnailFilename'
        WHERE private_token = '" . mysql_real_escape_string($privateToken) . "'";

	mysql_query($updateQuery);

    //echo $updateQuery;
}
else {
    // No token provided - save a new record

	$privateToken =  uniqid();
	$publicToken = uniqid();

    if ($imageData) {
        // Save the new image contents
        $fullFilename = saveImage($imageData, $privateToken);
    }

	$insertQuery =
       "INSERT INTO design(name, content, description, public_token, private_token, image, thumbnail_image, created_date, edited_date, email, size_width, size_length)
        VALUES ('" . mysql_real_escape_string($name) . "', '" . mysql_real_escape_string($content) . "', '" . mysql_real_escape_string($description) . "', '$publicToken', '$privateToken', '', '', CURRENT_DATE(), CURRENT_DATE(), '" . mysql_real_escape_string($email) . "', '" . mysql_real_escape_string($sizeWidth) . "', '" . mysql_real_escape_string($sizeHeight) . "')";

	mysql_query($insertQuery);

    //echo $insertQuery;

    echo json_encode(array(
        "privateToken" => $privateToken,
        "publicToken" => $publicToken,
    ));
}
